package com.zhiwen.config;

import com.zhiwen.distributed.DistributedLocker;
import com.zhiwen.distributed.impl.RedissonDistributedLocker;
import com.zhiwen.util.RedissLockUtil;
import org.apache.commons.lang.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.MasterSlaveServersConfig;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Auther: wuzhiwen
 * @Date: 2018/11/9 15:48
 * @Description:
 */
@Configuration
@ConditionalOnClass(Config.class)
public class RedissonManager {

    @Autowired
    private RedissonProperties redssionProperties;

//    /**
//     * 哨兵模式自动装配
//     * @return
//     */
//    @Bean
//    @ConditionalOnProperty(name="redisson.master-name")
//    RedissonClient redissonSentinel() {
//        Config config = new Config();
//        SentinelServersConfig serverConfig = config.useSentinelServers().addSentinelAddress(redssionProperties.getSentinelAddresses())
//                .setMasterName(redssionProperties.getMasterName())
//                .setTimeout(redssionProperties.getTimeout())
//                .setMasterConnectionPoolSize(redssionProperties.getMasterConnectionPoolSize())
//                .setSlaveConnectionPoolSize(redssionProperties.getSlaveConnectionPoolSize());
//
//        if(StringUtils.isNotBlank(redssionProperties.getPassword())) {
//            serverConfig.setPassword(redssionProperties.getPassword());
//        }
//        return Redisson.create(config);
//    }

//    /**
//     * 集群模式自动装配
//     * @return
//     */
//    @Bean
//    @ConditionalOnProperty(name="redisson.cluster-server")
//    RedissonClient redissoClusterServers() {
//        Config config = new Config();
//        ClusterServersConfig serverConfig = config.useClusterServers().addNodeAddress(redssionProperties.getClusterAddresses())
//                .setScanInterval(redssionProperties.getScanIntervalTime())
//                .setTimeout(redssionProperties.getTimeout());
//
//        if(StringUtils.isNotBlank(redssionProperties.getPassword())) {
//            serverConfig.setPassword(redssionProperties.getPassword());
//        }
//        return Redisson.create(config);
//    }

//    /**
//     * 主从模式自动装配
//     * @return
//     */
//    @Bean
//    @ConditionalOnProperty(name="redisson.cluster-server")
//    RedissonClient redissoClusterServers() {
//        Config config = new Config();
//        MasterSlaveServersConfig serverConfig = config.useMasterSlaveServers().setMasterAddress(redssionProperties.getMasterAddress())
//                .addSlaveAddress(redssionProperties.getSlaveAddresses())
//                .setTimeout(redssionProperties.getTimeout());
//
//        if(StringUtils.isNotBlank(redssionProperties.getPassword())) {
//            serverConfig.setPassword(redssionProperties.getPassword());
//        }
//        return Redisson.create(config);
//    }

    /**
     * 单机模式自动装配
     * @return
     */
    @Bean
    @ConditionalOnProperty(name="redisson.address")
    RedissonClient redissonSingle() {
        Config config = new Config();
        SingleServerConfig serverConfig = config.useSingleServer()
                .setAddress(redssionProperties.getAddress())
                .setTimeout(redssionProperties.getTimeout())
                .setConnectionPoolSize(redssionProperties.getConnectionPoolSize())
                .setConnectionMinimumIdleSize(redssionProperties.getConnectionMinimumIdleSize());

        if(StringUtils.isNotBlank(redssionProperties.getPassword())) {
            serverConfig.setPassword(redssionProperties.getPassword());
        }

        return Redisson.create(config);
    }

    /**
     * 装配locker类，并将实例注入到RedissLockUtil中
     * @return
     */
    @Bean
    DistributedLocker distributedLocker(RedissonClient redissonClient) {
        DistributedLocker locker = new RedissonDistributedLocker();
        locker.setRedissonClient(redissonClient);
        RedissLockUtil.setLocker(locker);
        return locker;
    }

}
